import request from "./axios";

function loginApi({ username, password }) {
  return request.post("/user/login", {
    username,
    password,
  });
}
function registerApi({ username, password }) {
  return request.post("/user/register", {
    username,
    password,
  });
}

function getUserInfoApi() {
  return request.get("/user");
}

function getPageApi(uuid, pageSize, current, searchText, sortField, sortOrder) {
  return request.get(`/file/page/${uuid ?? ""}`, {
    pageSize,
    current,
    searchText,
    sortField,
    sortOrder,
  });
}

function updateUserApi(data) {
  return request.patch("/user", data);
}

function updatePasswordApi(data) {
  return request.patch("/user/password", data);
}

function deleteFileApi(fileId) {
  return request.del(`/file/${fileId}`);
}

function deleteFilesApi(fileIds) {
  return request.del("/file/batch", { fileIds });
}

function recycleFileApi(fileId) {
  return request.patch(`/file/recycle/${fileId}`);
}

function recycleFilesApi(fileIds) {
  return request.patch("/file/recycle/batch", { fileIds });
}

function createFolderApi(parentId, filename) {
  return request.post(`/file/folder/${parentId ?? ""}`, { filename });
}
function renameApi(fileId, filename) {
  return request.patch(`/file/filename/${fileId ?? ""}`, { filename });
}


async function downloadFileApi(id, filename) {
  const url = `/file/download/${id ?? ""}`;
  try {
    console.log("下载文件", url);
    const response = await request.getBlob(url);
    const downloadUrl = window.URL.createObjectURL(response);
    const a = document.createElement("a");
    a.href = downloadUrl;
    a.download = filename; // 设置下载文件名
    a.click();
    // 释放内存
    window.URL.revokeObjectURL(downloadUrl);
  } catch (error) {
    console.error("下载失败:", error);
  }
}

async function downloadShareApi(id) {
  const url = `/share/download/${id ?? ""}`;
  try {
    console.log("下载分享", url);
    const response = await request.getBlob(url);
    const downloadUrl = window.URL.createObjectURL(response);
    const a = document.createElement("a");
    a.href = downloadUrl;
    a.download = filename; // 设置下载文件名
    a.click();
    // 释放内存
    window.URL.revokeObjectURL(downloadUrl);
  } catch (error) {
    console.error("下载失败:", error);
  }
}

async function getFileUrlApi(id) {
  return request.get(`/file/url/${id}`);
}

async function getShareUrlApi(id, pwd) {
  return request.post(`/share/url/${id}`, { pwd });
}

function getFileInfoApi(id) {
  return request.get(`/file/${id}`);
}

function shareApi(fileId, pwd, expires) {
  return request.post(`/share`, { fileId, pwd, expires });
}

function getSharesApi() {
  return request.get(`/share`);
}

function getShareByIdApi(id) {
  return request.get(`/share/${id}`);
}

function verifyShareApi(id, pwd) {
  return request.post(`/share/verify/${id}`, { pwd });
}

function deleteShareApi(id) {
  return request.del(`/share/${id}`);
}

function deleteSharesApi(shareIds) {
  return request.del(`/share/batch`, { shareIds });
}

function getFoldersApi(parentId) {
  return request.get(`/file/folders/${parentId ?? ""}`);
}

function moveFileApi(fileId, parentId) {
  return request.patch(`/file/move/${fileId ?? ""}`, { parentId });
}

function moveFilesApi(fileIds, parentId) {
  return request.patch(`/file/move/batch`, { fileIds, parentId });
}

function getRecycleApi() {
  return request.get(`/file/recycle`);
}

function restoreFileApi(fileId) {
  return request.patch(`/file/restore/${fileId}`);
}

function restoreFilesApi(fileIds) {
  return request.patch(`/file/restore/batch`, { fileIds });
}

function verifyUploadApi(hash, filename) {
  return request.post(`/upload/verify`, { hash, filename });
}

function mergeFileApi(hash, filename, size, type, parentId) {
  return request.post(`/upload/merge`, {
    hash,
    filename,
    size,
    type,
    parentId,
  });
}

function getCoverApi(hash) {
  return request.getBlob(`/upload/${hash}/cover`);
}

function downloadFolderApi(folderId) {
  return request.get(`/file/download/folder/${folderId ?? ""}`);
}

function downloadFilesApi(Ids) {
  return request.post(`/file/download/batch`, { Ids });
}

function copyFileApi(fileId, parentId) {
  return request.post(`/file/copy/${fileId ?? ""}`, { parentId });
}

function saveShareApi(shareId, pwd, parentId) {
  return request.post(`/share/save/${shareId ?? ""}`, { pwd, parentId });
}

function getUsersApi() {
  return request.get(`/admin/users`);
}

function getOverviewApi() {
  return request.get(`/admin/overview`);
}

function getRecentDaysApi() {
  return request.get(`/admin/recent`);
}

function getPropertyApi() {
  return request.get(`/admin/property`);
}

function getLogsApi() {
  return request.get(`/admin/logs`);
}

function getTenUsersApi() {
  return request.get(`/admin/tenUsers`);
}

function updateSpaceApi(id, storage) {
  return request.patch(`/admin/space`, { id, storage });
}

function updateStatusApi(id, status) {
  return request.patch(`/admin/status`, { id, status });
}

function updateRoleApi(id, role) {
  return request.patch(`/admin/role`, { id, role });
}

function getConfigApi() {
  return request.get(`/admin/config`);
}

function updateConfigApi(config) {
  return request.patch(`/admin/config`, config);
}

function getUploadSizeLimitApi() {
  return request.get(`/upload/config`);
}

export {
  loginApi,
  registerApi,
  getUserInfoApi,
  getPageApi,
  updateUserApi,
  updatePasswordApi,
  deleteFileApi,
  deleteFilesApi,
  recycleFileApi,
  recycleFilesApi,
  createFolderApi,
  renameApi,
  downloadFileApi,
  downloadShareApi,
  getFileInfoApi,
  shareApi,
  getSharesApi,
  getShareByIdApi,
  verifyShareApi,
  deleteShareApi,
  getFoldersApi,
  moveFileApi,
  moveFilesApi,
  getRecycleApi,
  restoreFileApi,
  restoreFilesApi,
  deleteSharesApi,
  verifyUploadApi,
  mergeFileApi,
  getCoverApi,
  getFileUrlApi,
  getShareUrlApi,
  downloadFolderApi,
  downloadFilesApi,
  copyFileApi,
  saveShareApi,
  getUsersApi,
  getOverviewApi,
  getRecentDaysApi,
  getPropertyApi,
  getLogsApi,
  getTenUsersApi,
  updateSpaceApi,
  updateStatusApi,
  updateRoleApi,
  getConfigApi,
  updateConfigApi,
  getUploadSizeLimitApi
};
